/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.epfresh.R;
import com.epfresh.bean.StoreDetail;

import java.util.ArrayList;
import java.util.List;

import multitype.ItemViewBinder;
import com.epfresh.fragment.*;

public class ViewBinderHomeViewPager
    extends ItemViewBinder<List<StoreDetail>, ViewBinderHomeViewPager.ViewHolder> {
    Context context;
    ViewPager viewpager_home_;
    ViewGroup llDcFlag;
    HomeViewPagerAdapter homeViewPagerAdapter;
    ArrayList<StoreDetail> dCList = new ArrayList();
    List<View> viewLists = new ArrayList<>();

    private View.OnClickListener onItemClickListener;

    public ViewBinderHomeViewPager(Context context) {
        this.context = context;
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @NonNull @Override
    protected ViewHolder onCreateViewHolder(
        @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.home_menu_vp, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewpager_home_ = viewHolder.viewpager_home_;

        llDcFlag = viewHolder.llDcFlag;
        return viewHolder;
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull List<StoreDetail> category) {


        if (homeViewPagerAdapter == null) {
            homeViewPagerAdapter = new HomeViewPagerAdapter(viewLists, dCList);
            viewpager_home_.setAdapter(homeViewPagerAdapter);
        }
        homeViewPagerAdapter.setOnItemClickListener(onItemClickListener);
        if(lastList!=null&&lastList.size()==category.size()){
            if(lastList.size()==0) {
                //Log.e("dCList.size()", "_____屏蔽更新");//连续没有配送中心屏蔽更新
                return;
            } else if (lastList.size()>1&&lastList.get(0).getId()!=null&&lastList.get(0).getId().equals(category.get(0).getId())) {//两次都有配送中心，但是第一个配送中心的id一致屏蔽更新
                //Log.e("dCList.size()", "_____屏蔽更新");
            }
        }
        lastList = category;
        update(category,holder);
    }
    List<StoreDetail> lastList;


    public void update(List<StoreDetail> storeDetailList,ViewHolder holder ) {

        this.dCList.clear();
        viewLists.clear();

        if (storeDetailList == null || storeDetailList.size() == 0) {
            initTipsViewDc(llDcFlag, null);
            viewLists.add(0, LayoutInflater.from(context).inflate(R.layout.include_home_view_page, null, false));
            homeViewPagerAdapter.notifyDataSetChanged();
            return;
        } else {
            this.dCList.addAll(storeDetailList);
            for (int i = 0; i <= (storeDetailList.size() - 1) / 4 + 1; i++) {
                viewLists.add(i, LayoutInflater.from(context).inflate(R.layout.include_home_view_page, null, false));
            }
            holder.flags = new ImageView[viewLists.size()];
            //Log.e("viewList.size()", "_____" + viewLists.size());
            if (onItemClickListener instanceof TopFragmentRecycler) {
                //((TopFragmentRecycler) onItemClickListener).showTips(); 不再提示滑动查看更多配送中心2018年4月27日16:07:16
            }
            initTipsViewDc(llDcFlag, holder.flags);
            homeViewPagerAdapter.notifyDataSetChanged();
        }
    }


    void initTipsViewDc(ViewGroup llBannerFlag, ImageView[] tips) {
        if (llBannerFlag == null) {
            return;
        }
        llBannerFlag.removeAllViews();
        if (tips != null) {
            for (int i = 0; i < tips.length; i++) {
                final ImageView imageView = new ImageView(context);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(10, 10, 0, 0);
                imageView.setLayoutParams(lp);
                // imageView.setLayoutParams(new LayoutParams(20, 20));
                tips[i] = imageView;
                if (i == viewpager_home_.getCurrentItem()) {
                    tips[i].setBackgroundResource(R.mipmap.page_tip_1);
                } else {
                    tips[i].setBackgroundResource(R.mipmap.page_tip_2);
                }

                llBannerFlag.addView(imageView);
            }
        }
    }
    /**
     * 设置选中的tip的背景
     *
     * @param selectItemsIndex
     */
    private void setImageBackgroundDc(int selectItemsIndex, ImageView[] tips) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItemsIndex) {
                tips[i].setBackgroundResource(R.mipmap.page_tip_1);
            } else {
                tips[i].setBackgroundResource(R.mipmap.page_tip_2);
            }
        }
    }
    class ViewHolder extends RecyclerView.ViewHolder {

        ViewPager viewpager_home_;
        ViewGroup llDcFlag;
        ImageView[] flags;

        ViewHolder(@NonNull View view) {
            super(view);
            viewpager_home_ = (ViewPager) view.findViewById(R.id.viewpager_dc);
            llDcFlag = (ViewGroup) view.findViewById(R.id.ll_dc_flag);

            viewpager_home_.setAdapter(homeViewPagerAdapter);
            viewpager_home_.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrollStateChanged(int arg0) {
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }

                @Override
                public void onPageSelected(int arg0) {
                    setImageBackgroundDc(arg0, flags);
                }
            });

        }
    }

}
